package registration;


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;



import logic.*;



public class DataBase implements IDatabaseConnect{
        
        private Statement state = null;
        private ResultSet result = null;
        private PreparedStatement prepar = null;
        int id_client;
        String mail;
		private PreparedStatement preparedStatement = null;
        
        
        public DataBase() {}

       /**
        * Metoda sprawdzajaca czy podany login i adres e-mail znajduje sie w bazie danych
        * 
        * 
        * @param login
        * @param mail
        * @param request
        * @return
        * @throws SQLException
        */
        public boolean checkLoginAndMail(String login, String mail, HttpServletRequest request) throws SQLException{
        	ConnectPRO.connectpro();
        	preparedStatement  = ConnectPRO.connect.prepareStatement("select login, mail from CLIENTS where login = ? or mail = ?");
			preparedStatement.setString(1, login);
			preparedStatement.setString(2, mail);
			result = preparedStatement.executeQuery();
			String tekst = " ";
			String tekst1 = " ";
			boolean check = false;
			if(result != null){
				while(result.next()){
					if(login.equals(result.getString("login"))){
						tekst = "Podany login jest zajety";
						request.setAttribute("validate", tekst);
						check =  true;
					}
					if(mail.equals(result.getString("mail"))){
						tekst1 = "Podany adres mail jest juz zajety";
						request.setAttribute("validate1", tekst1);
						check = true;
					}
										
				}
				
			}
			else{
				System.out.println("Wszystko spoko!");
			}
        	System.out.println("check: "+check + " " + tekst +" " + tekst1);
			return check; 
        	
        }

        
        //metoda pobierajaca id_clienta z bazy wg loginu
        public int getId_client(String login) throws SQLException {
                ConnectPRO.connectpro();
                state = ConnectPRO.connect.createStatement();           
                result = state.executeQuery("select login, id_client from CLIENTS where login = '" + login + "'");
                while(result.next()){
                        id_client = result.getInt("id_client");
                }
                return id_client;
        }
        
        
        //metoda pobierajaca maila z bazy wg loginu
        public String getMail_client(String login) throws SQLException {
                ConnectPRO.connectpro();
                state = ConnectPRO.connect.createStatement();           
                result = state.executeQuery("select login, mail from CLIENTS where login = '" + login + "'");
                while(result.next()){
                        mail = result.getString("mail");
                }
                return mail;
        }
        
        
        //metoda pobierajaca cokolwiek z bazy wg loginu
        public String getWhateverString(String login, String whateverString) throws SQLException {
                ConnectPRO.connectpro();
                state = ConnectPRO.connect.createStatement();           
                result = state.executeQuery("select * from CLIENTS where login = '" + login + "'");
                while(result.next()){
                	whateverString = result.getString(whateverString);
                }
                return whateverString;
        }
        
        
        /**
         * metoda zapisujaca uzytkownika w bazie danych
         * 
         * @param login
         * @param password
         * @param name
         * @param surname
         * @param phone
         * @param mail
         * @param street
         * @param no_1
         * @param no_2
         * @param post_code
         * @param city
         * @throws SQLException
         * @throws ClassNotFoundException
         */
        public void registerUser(String login, String password, String name,
                                String surname, Integer phone, String mail, String street,
                                Integer no_1, String no_2, String post_code, String city) throws SQLException, ClassNotFoundException {
                
                ConnectPRO.connectpro();
                                        
                prepar = ConnectPRO.connect.prepareStatement("insert into CLIENTS values(default, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?)");
                prepar.setString(1, login);
                prepar.setString(2, password);
                prepar.setString(3, name);
                prepar.setString(4, surname);
                prepar.setInt(5, phone);
                prepar.setString(6, mail);
                prepar.setString(7, street);
                prepar.setInt(8, no_1);
                prepar.setString(9, no_2);
                prepar.setString(10, post_code);
                prepar.setString(11, city);
                prepar.executeUpdate();
                
                //close();      
                
        }


}